<template>
  <div class="article-comment">
    <!-- 全部评论 -->
    <van-sticky offset-top="11.73333vw">
      <div class="title van-hairline--bottom">
        <span>全部评论 ({{ article.comm_count }})</span>
        <span>{{ article.like_count }} 点赞</span>
      </div>
    </van-sticky>
    <!-- 评论列表 -->
    <div class="list">
      <van-list
        v-model="loading"
        :error.sync="error"
        error-text="请求失败，点击重新加载"
        :finished="finished"
        finished-text="没有评论了"
        @load="onLoad"
      >
        <div
          class="item van-hairline--bottom"
          v-for="item in comments"
          :key="item.com_id"
        >
          <van-image round width="10vw" height="10vw" :src="item.aut_photo" />
          <div class="info">
            <p>
              <span class="name">{{ item.aut_name }}</span>
              <span class="zan"
                >{{ item.like_count }}
                <i
                  class="iconfont"
                  :class="item.is_liking ? 'icon-like-sel' : 'icon-like2'"
                />
              </span>
            </p>
            <p class="cont">{{ item.content }}</p>
            <p>
              <span class="reply"
                >{{ item.reply_count }}回复
                <i class="van-icon van-icon-arrow"></i
              ></span>
              <span class="time">{{ item.pubdate | relativeTime }}</span>
            </p>
          </div>
        </div>
      </van-list>
    </div>
    <!-- 底部工具 -->
    <!-- 底部工具 -->
    <div class="footer van-hairline--top">
      <div class="input"><i class="van-icon van-icon-edit"></i></div>
      <div class="btn" @click="$emit('click-comment')">
        <i class="iconfont icon-comment"></i>
        <p>评论</p>
        <i v-if="article.comm_count">{{ article.comm_count }}</i>
      </div>
      <div class="btn" @click="likeArticle">
        <i
          class="iconfont"
          :class="article.attitude === 1 ? 'icon-like-sel' : 'icon-like'"
        ></i>
        <p>点赞</p>
      </div>
      <div class="btn" @click="collectArticle">
        <i
          class="iconfont"
          :class="article.is_collected ? 'icon-collect-sel' : 'icon-collect'"
        ></i>
        <p>收藏</p>
      </div>
      <div class="btn">
        <i class="iconfont icon-share"></i>
        <p>分享</p>
      </div>
    </div>
  </div>
</template>

<script>
import { getCommentsByArticle, likeArticle, collectArticle } from '@/api/article'

// import { likeArticle } from '@/api/articles'
export default {
  name: 'ArticleComment',
  props: {
    article: {
      require: true,
      default: () => {}
    }
  },
  data () {
    return {
      offset: null,
      comments: [],
      loading: false,
      error: false,
      finished: false
    }
  },
  created () {
    // 开启加载中效果
    this.loading = true
    this.onLoad()
  },
  methods: {
    async likeArticle () {
      if (this.article.attitude === 1) {
        const [err] = await likeArticle(this.article.art_id, false)
        if (err) return this.$toast.fail('操作失败')
        // eslint-disable-next-line vue/no-mutating-props
        this.article.attitude = -1
        this.$toast.success('取消成功')
      } else {
        const [err] = await likeArticle(this.article.art_id, true)
        if (err) return this.$toast.fail('操作失败')
        // eslint-disable-next-line vue/no-mutating-props
        this.article.attitude = 1
        this.$toast.success('点赞成功')
      }
    },
    // 收藏
    async collectArticle () {
      if (this.article.is_collected) {
        const [err] = await collectArticle(this.article.art_id, false)
        if (err) return this.$toast.fail('操作失败')
        // eslint-disable-next-line vue/no-mutating-props
        this.article.is_collected = false
        this.$toast.success('取消成功')
      } else {
        const [err] = await collectArticle(this.article.art_id, true)
        if (err) return this.$toast.fail('操作失败')
        // eslint-disable-next-line vue/no-mutating-props
        this.article.is_collected = true
        this.$toast.success('收藏成功')
      }
    },
    async onLoad () {
      // 获取数据
      const [err, res] = await getCommentsByArticle(
        this.article.art_id,
        this.offset
      )
      // 如果有错误，失败
      if (err) {
        // 显示错误状态
        this.error = true
        // 否则，成功
      } else {
        // 如果这次数据最后一条ID和所有数据最后一条ID相同，没有数据了
        if (res.data.data.last_id === res.data.data.end_id) {
          // 加载完毕
          this.finished = true
          // 否则，还有数据
        } else {
          // 记录偏移量，为下次请求准备
          this.offset = res.data.data.last_id
        }
        // 追加评论数据
        this.comments.push(...res.data.data.results)
      }
      // 不管成功失败，都要结束加载状态
      this.loading = false
    }
  }
}
</script>

<style scoped lang="less">
.article-comment {
  .title {
    height: 50px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 16px;
    background: #fff;
    span {
      font-size: 16px;
      &:last-child {
        color: #ccc;
        font-size: 14px;
      }
    }
  }
  .footer {
    position: fixed;
    left: 0;
    bottom: 0;
    height: 50px;
    background: #fff;
    display: flex;
    width: 100%;
    align-items: center;
    .input {
      margin-left: 10px;
      width: 200px;
      height: 34px;
      background: @geek-gray-color;
      border-radius: 17px;
      line-height: 36px;
      padding-left: 10px;
      .van-icon {
        color: #999;
      }
    }
    .btn {
      flex: 1;
      text-align: center;
      position: relative;
      p {
        margin: 0;
        font-size: 10px;
      }
      .iconfont {
        font-size: 18px;
      }
      i:not(.iconfont) {
        height: 16px;
        min-width: 16px;
        padding: 0 3px;
        background: @geek-color;
        color: #fff;
        font-size: 10px;
        position: absolute;
        right: 0;
        top: -4px;
        line-height: 16px;
        border-radius: 8px;
        font-style: normal;
      }
    }
  }
  .list {
    padding: 0 16px;
    .item {
      display: flex;
      padding: 10px 0;
      .info {
        padding-left: 10px;
        flex: 1;
        p {
          margin: 0;
          .name {
            font-size: 16px;
          }
          .zan {
            font-size: 14px;
            float: right;
            color: #999;
            .i {
              font-size: 12px;
              position: relative;
              top: -1px;
            }
          }
          &.cont {
            font-size: 14px;
            color: #666;
            padding: 10px 0;
            word-break: break-all;
            padding-right: 40px;
          }
          .reply {
            min-width: 60px;
            height: 24px;
            text-align: center;
            line-height: 28px;
            font-size: 12px;
            background: @geek-gray-color;
            display: inline-block;
            border-radius: 14px;
            color: #666;
            .van-icon {
              position: relative;
              top: 1px;
            }
          }
          .time {
            font-size: 12px;
            color: #999;
            margin-left: 10px;
          }
        }
      }
    }
  }
}
</style>
